

* Clean out unwanted variables from student data

	use "$basefiles\\$basefile_students", clear
	
	keep stu_id year sch_id ward grade dob age resid male ///
	white black gender_miss hisp othrace race_miss lep_dv lep_miss sped_dv sped_miss ///
	frpl_dv frpl_miss homelang math_grdsubj math_std math_std_t math_pred math_resid ///
	read_grdsubj read_std read_std_t read_pred read_resid ///
	membership_days absences totalexc_abs totalunexc_abs unexctruancy_abs absences_t ///
	exp susp stonsitesusp stoffsitesusp stsusp mtsusp ltsusp suspdays
	
	keep if year > 2009 & year < 2013
	
	save "$temp_dir\tmp_students.dta", replace
	
	* 2011 and 2012 only
	*exp susp stonsitesusp stoffsitesusp stsusp mtsusp ltsusp suspdays
	
	******************************************
	* 1. Merge student -> roster <- teacher
	******************************************
	
	use "$basefiles\\$basefile_studentRoster", clear
	*drop if year_dose == 0
	keep if year > 2009 & year < 2013
	
	merge m:1 stu_id year using "$temp_dir\tmp_students.dta"
	tab _merge year
	drop if _merge != 3
	drop _merge
		
	merge m:1 id year using "$input_dir\teacher_threat_noiva_withvars.dta"
	tab _merge year
	keep if _merge == 3
	
	* add variables
	
	duplicates tag stu_id id year, gen(teacher_twice)
	encode math_grdsubj, gen(math_grdsubj_c)
	encode read_grdsubj, gen(read_grdsubj_c)
	
	save  "$input_dir\students_threat.dta", replace
	
	